/*          图库(相册)界面 画板保存截图展示               */
#include <stdio.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <pthread.h>
#include <stdbool.h>
#include <sys/mman.h>
#include <linux/input.h>

#include "gallery_interface.h"
#include "bmp_dir.h"

int gallery_interface(char *account)
{
    int x = -1, y = -1;             // 坐标初始值是-1
    struct input_event touch_event; // 触摸屏数据结构体

    int lcd_fd = open("/dev/fb0", O_RDWR);
    if (lcd_fd < 0)
    {
        perror("Failed to open frame buffer device");
        return -1;
    }
    int(*lcd_mmap)[WIDTH] = (int(*)[WIDTH])mmap(NULL, WIDTH * HEIGHT * 4, PROT_READ | PROT_WRITE, MAP_SHARED, lcd_fd, 0);

    lcd_clear(); // 清屏

    // 打开触摸屏设备文件
    int touch_fd = open("/dev/input/event0", O_RDWR);
    if (touch_fd < 0)
    {
        perror("打开触摸设备失败");
        return -1;
    }

    int empty = bmp_arr(account); // 判断是否获取到图片并存到数组中
    int index = 0;                // 当前图片索引

    if (empty == 0)
    {
        show_bmp(bmp_files[0]->filename, 0, 0);
        show_album_i();
    }
    else if (empty != 0 || bmp_files_size == 0) // 当没有图片
    {
        show_bmp("./res/album_empty.bmp", 0, 0);
        show_bmp("./res/exit.bmp", 0, 0);
    }

    while (1)
    {
        // 读取触摸屏设备文件数据
        int read_ret = read(touch_fd, &touch_event, sizeof(struct input_event));
        if (read_ret < 0)
        {
            perror("读取触摸事件失败");
            return -1;
        }

        // 获取每次触摸坐标
        else if (touch_event.type == EV_ABS)
        {
            switch (touch_event.code)
            {
            case ABS_X:
                x = touch_event.value;
                break;
            case ABS_Y:
                y = touch_event.value;
                break;
            }
        }

        if (touch_event.type == EV_KEY)
        {
            if (touch_event.code == BTN_TOUCH && touch_event.value == 1)
            {
                if (empty == 0)
                {
                    if (x > 700 && y < 160 && bmp_files_size) // 点击删除
                    {
                        printf("删除图片%s\n", bmp_files[index]->filename);
                        removeBmpFileFromArray(index);
                        if (bmp_files_size == 0)
                        {
                            show_bmp("./res/album_empty.bmp", 0, 0);
                            show_bmp("./res/exit.bmp", 0, 0);
                        }
                        else
                        {
                            if (index == bmp_files_size)
                            {
                                index--;
                            }
                            show_bmp(bmp_files[index]->filename, 0, 0);
                        }
                    }
                    else if (x > 700 && y > 160 && y < 320 && bmp_files_size) // 点击下一张
                    {
                        index = (index + 1) % bmp_files_size;
                        show_bmp(bmp_files[index]->filename, 0, 0);
                        

                        show_album_i();
                    }
                    else if (x > 700 && y > 320 && bmp_files_size) // 点击上一张
                    {
                        index = (index + bmp_files_size - 1) % bmp_files_size;
                        show_bmp(bmp_files[index]->filename, 0, 0);

                        show_album_i();
                    }
                }

                if (y <= 40 && x <= 40) // 点击退出
                    goto exit;
            }
        }
    }

exit:
    close(touch_fd);
    munmap(lcd_mmap, WIDTH * HEIGHT * 4);
    close(lcd_fd);

    return 0;
}

// 图库图标显示
void show_album_i()
{
    show_bmp("./res/album_next.bmp", 700, 0);
    show_bmp("./res/delete.bmp", 700, 40);
    show_bmp("./res/next.bmp", 700, 200);
    show_bmp("./res/prev.bmp", 700, 360);
    show_bmp("./res/exit.bmp", 0, 0);
}